/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.mallsite.modules.shop.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.sys.utils.UserUtils;
import com.mallsite.modules.shop.entity.Goods;
import com.mallsite.modules.shop.service.GoodsService;

/**
 * 商品Controller
 * @author ruyang
 * @version 2020-12-04
 */
@Controller
@RequestMapping(value = "${adminPath}/shop/goods")
public class GoodsController extends BaseController {

	@Autowired
	private GoodsService goodsService;
	
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public Goods get(String goodsId, boolean isNewRecord) {
		Goods goods = goodsService.get(goodsId, isNewRecord);
		return goods;
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("shop:goods:view")
	@RequestMapping(value = {"list", ""})
	public String list(Goods goods, Model model) {
		model.addAttribute("goods", goods);
		model.addAttribute("user", UserUtils.getUser());
		return "modules/shop/goodsList";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("shop:goods:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<Goods> listData(Goods goods, HttpServletRequest request, HttpServletResponse response) {
		Page<Goods> page = goodsService.findPage(new Page<Goods>(request, response), goods); 
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("shop:goods:view")
	@RequestMapping(value = "form")
	public String form(Goods goods, Model model) {
		model.addAttribute("goods", goods);
		model.addAttribute("user", UserUtils.getUser());
		return "modules/shop/goodsForm";
	}

	/**
	 * 保存商品
	 */
	@RequiresPermissions("shop:goods:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated Goods goods) {
		goodsService.save(goods);
		return renderResult(Global.TRUE, text("保存商品成功！"));
	}
	
	/**
	 * 停用商品
	 */
	@RequiresPermissions("shop:goods:edit")
	@RequestMapping(value = "disable")
	@ResponseBody
	public String disable(Goods goods) {
		goods.setStatus(Goods.STATUS_DISABLE);
		goodsService.updateStatus(goods);
		return renderResult(Global.TRUE, text("停用商品成功"));
	}
	
	/**
	 * 启用商品
	 */
	@RequiresPermissions("shop:goods:edit")
	@RequestMapping(value = "enable")
	@ResponseBody
	public String enable(Goods goods) {
		goods.setStatus(Goods.STATUS_NORMAL);
		goodsService.updateStatus(goods);
		return renderResult(Global.TRUE, text("启用商品成功"));
	}
	
	/**
	 * 删除商品
	 */
	@RequiresPermissions("shop:goods:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(Goods goods) {
		goodsService.delete(goods);
		return renderResult(Global.TRUE, text("删除商品成功！"));
	}
	
}